<template>
  <a-card :bordered='false'>

    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline'>
        <a-row :gutter='12'>
          <a-col :md='4' :sm='8'>
            <a-form-item label='所属项目'>
              <a-select show-search :filter-option='filterOption' v-model='queryParam.projectId' placeholder='请输入所属项目'
                        style='margin-right: 20px;width: 100%'>
                <a-select-option value=''>请选择项目</a-select-option>
                <a-select-option v-for='item in projectList' :label='item.name' :value='item.id' :key='item.id'>
                  <span style='float: left'>{{ item.name }}</span>
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='4' :sm='8'>
            <a-form-item label='姓名'>
              <a-input placeholder='请输入姓名' v-model='queryParam.name'></a-input>
            </a-form-item>
          </a-col>
          <a-col :md='5' :sm='8'>
            <a-form-item label='卡号/密码/身份证'>
              <a-input placeholder='请输入卡号' v-model='queryParam.cardNumber'></a-input>
            </a-form-item>
          </a-col>
          <a-col :md='4' :sm='8'>
            <a-form-item label='手机号'>
              <a-input placeholder='请输入手机号' v-model='queryParam.phoneNumber'></a-input>
            </a-form-item>
          </a-col>
          <a-col :md='4' :sm='8'>
            <a-form-item label='设备序号'>
              <a-input placeholder='请输入设备序号' v-model='queryParam.deviceId'></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :md='4' :sm='4'>
            <a-form-item label='开门时间'>
              <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' placeholder='开门时间' v-model='queryParam.opTime_begin'
                             @change='onChangeStartTime' />
            </a-form-item>
          </a-col>

          <a-col :md='4' :sm='4'>
            <a-form-item>
              <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' placeholder='结束日期' v-model='queryParam.opTime_end'
                             @change='onChangeEndTime' />
            </a-form-item>
          </a-col>
          <a-col :md='4' :sm='8'>
            <span style='float: left;overflow: hidden;' class='table-page-search-submitButtons'>
              <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
              <a-button type='primary' @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- table区域-begin -->
    <div>
      <a-table
        ref='table'
        size='middle'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        @change='handleTableChange'>

        <span slot='action' slot-scope='text, record'>
          <a @click='handleEdit(record)'>编辑</a>

          <a-divider type='vertical' />
          <a-dropdown>
            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
            <a-menu slot='overlay'>
              <a-menu-item>
                <a-popconfirm title='确定删除吗?' @confirm='() => handleDelete(record.id)'>
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        <span slot='doorOpeningType' slot-scope='record'>
<span v-if='record===1'>人脸识别</span>
<span v-if='record===2'>韦根卡识别</span>
<span v-if='record===3'>身份证识别</span>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <doorOpeningRecord-modal ref='modalForm' @ok='modalFormOk'></doorOpeningRecord-modal>
  </a-card>
</template>

<script>
import DoorOpeningRecordModal from './modules/DoorOpeningRecordModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { deleteAction, getAction, downFile } from '@api/manage'
import { filterObj } from '@/utils/util'
import { getProjectList } from '@api/api'

export default {
  name: 'DoorOpeningRecordList',
  mixins: [JeecgListMixin],
  components: {
    DoorOpeningRecordModal
  },
  data() {
    return {
      description: '开门记录管理页面',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'projectName'
        },
        {
          title: '姓名',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: '体表温度',
          align: 'center',
          dataIndex: 'bodySurfaceTemperature'
        },
        {
          title: '卡号',
          align: 'center',
          dataIndex: 'cardNumber'
        },
        {
          title: '电话号码',
          align: 'center',
          dataIndex: 'phoneNumber'
        },
        {
          title: '设备序列号',
          align: 'center',
          dataIndex: 'deviceId'
        },
        {
          title: '设备名称',
          align: 'center',
          dataIndex: 'deviceName'
        },
        {
          title: '开门类型',
          align: 'center',
          dataIndex: 'doorOpeningType',
          scopedSlots: { customRender: 'doorOpeningType' }
        },

        {
          title: '抓拍图片',
          width: 80,
          align: 'center',
          dataIndex: 'img',
          customRender: (record, row, index) => {
            let r = record
            return (
              <img
                style='max-width:35px;max-height:35px;min-height:20px;min-width:20px;text-align:center;cursor:pointer;border:1px #BFBFBF solid;padding: 2px 2px 2px 2px;background-color:white;'
                preview={r}
                src={record}
              ></img>
            )
          }
        },
        {
          title: '开门时间',
          align: 'center',
          dataIndex: 'openingTime'
        }
      ],
      url: {
        list: '/dooropeningrecord/doorOpeningRecord/list',
        delete: '/dooropeningrecord/doorOpeningRecord/delete',
        deleteBatch: '/dooropeningrecord/doorOpeningRecord/deleteBatch',
        exportXlsUrl: 'dooropeningrecord/doorOpeningRecord/exportXls',
        importExcelUrl: 'dooropeningrecord/doorOpeningRecord/importExcel'
      },
      queryParam: {

        'opTime_begin': '',
        'opTime_end': '',
        'type': '-1',
        'areaId': '',
        'name': '',
        'projectName': '',
        'accessType': '',
        'deviceId': '',
        'deviceName': '',
        'phoneNumber': '',
        'cardNumber': '',
        'bodySurfaceTemperature': '',
        'doorOpeningType': null

      },
      isorter: {
        column: 'createTime',
        order: 'desc'
      },
      projectList: [],
      doorOpeningType: [{ value: null, label: '请选择来访类型' }, { value: '1', label: '开门' }, { value: '2', label: '关门' }
      ]
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.keyupEnter()
    this.initProjectList()
  },
  methods: {
    initProjectList() {
      getProjectList().then((res) => {
        if (res.success) {
          this.projectList = res.result
        }
      })
    },
    filterOption(input, option) {
      let com = option.componentOptions
      let name = String(com.propsData.label)
      if (name.toLowerCase().includes(input.toLowerCase())) {
        return true
      }
    },
    searchReset: function() {
      this.queryParam = {
        'opTime_begin': '',
        'opTime_end': ''
      }
      this.loadData(1)
    },

    onChangeStartTime(date, dateString) {
      this.queryParam.opTime_begin = dateString
    },

    onChangeEndTime(date, dateString) {
      this.queryParam.opTime_end = dateString
    },


    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams()//查询条件
      this.loading = true
      console.log(params)
      getAction(this.url.list, params).then((res) => {
        if (res.success && res.result != null) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },

    searchQuery() {
      let startTime = this.queryParam.opTime_begin
      let endTime = this.queryParam.opTime_end
      if (startTime != '' && endTime != '') {
        if (startTime > endTime) {
          this.$message.error('开始时间不能大于结束时间')
          return
        }
      }
      if (startTime == '' && endTime != '') {
        this.$message.error('请选择开始时间')
        return
      }
      if (startTime != '' && endTime == '') {
        this.$message.error('请选择结束时间')
        return
      }
      this.loadData(1)
    },

    getQueryParams() {

      var param = Object.assign({}, this.isorter, this.queryParam)
      param.field = this.getQueryField()
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      if (this.queryParam.opTime_begin != '' && this.queryParam.opTime_end != '') {
        param.opTime_begin = this.queryParam.opTime_begin
        param.opTime_end = this.queryParam.opTime_end
      }

      // delete param.createTimeRange; // 时间参数不传递后台
      return filterObj(param)
    },


    batchDel: function() {

      if (!this.url.deleteBatch) {
        this.$message.error('请设置url.deleteBatch属性!')
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectionRows.length; a++) {
          ids += this.selectionRows[a].invateuserdetailId + ','
        }
        var that = this
        this.$confirm({
          title: '确认删除',
          content: '是否删除选中数据?',
          onOk: function() {
            deleteAction(that.url.deleteBatch, { ids: ids }).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          }
        })
      }
    },
    handleDelete: function(id) {
      if (!this.url.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      var that = this
      deleteAction(that.url.delete, { id: id }).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },


    keyupEnter() {
      document.onkeydown = e => {
        let body = document.getElementsByTagName('body')[0]
        if (e.keyCode === 13) {
          this.loadData(1)
        }
      }
    }

  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>